import {getApi} from '../../../api/common'
import {TOPIC, TOPIC_ARRAY} from './schema'
import {CALL_API} from "../../middlewares/normalizrMiddleware";
import {TOPIC_FETCH_ACTIONS, TOPICS_ACTIONS} from "../../../constants/ActionTypes";

/**
 * Created by Freeman on 2018/1/20.
 */
const fetchTopics = (id, number = 1) => (dispatch: Function) => {

  return dispatch({
    topicsType: id,
    [CALL_API]: {
      singlePage: false,
      types: TOPICS_ACTIONS,
      schema: TOPIC_ARRAY,
      api: getApi('/topics/search', {
        'page[number]': number,
        'page[size]': 6,
        'agg[size]': -1,
        'filter[channel]': id
      })
    }
  })
};

const fetchTopic = (id) => (dispatch: Function) => {
  return dispatch({
    [CALL_API]: {
      types: TOPIC_FETCH_ACTIONS,
      schema: TOPIC,
      api: getApi(`/topics/${id}`),
    }
  });
};

export default {
  fetchTopics,
  fetchTopic,
};
